package panels;

import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ListIterator;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import sqlmanager.FieldInfo;

public class ObjectBrowserPanel extends JPanel {
	// �������еİ�ť��action command
	private final String EditAC = "edit";
	private final String AddAC = "add";
	private final String DeleteAC = "delete";
	// ��FieldEditorPanel�еİ�ť��action command
	private final String ApplyAC = "apply";
	private final String CancelAC = "cancel";
	private final String HideAC = "hide";

	private JScrollPane scrollPane = null;
	private JSplitPane splitPane = null;
	public JTable table = null;
	private JToolBar toolBar = null;
	public JButton editButton = null;
	public JButton addButton = null;
	public JButton deleteButton = null;

	public ObjectBrowserModel tableModel = null; // @jve:decl-index=0:visual-constraint=""
	private FieldEditorPanel editPanel = null;
	private ButtonsActionHandler buttonsHandler = null;

	/**
	 * This is the default constructor
	 */
	public ObjectBrowserPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout()); // Generated
		this.setSize(300, 200);
		this.add(getToolBar(), java.awt.BorderLayout.NORTH); // Generated
		this.add(getSplitPane(), java.awt.BorderLayout.CENTER); // Generated
	}

	/**
	 * This method initializes scrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable()); // Generated
		}
		return scrollPane;
	}

	/**
	 * This method initializes table
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.setAutoCreateColumnsFromModel(true);
			table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); // Generated
			table.setModel(getTableModel()); // Generated
			table.getSelectionModel().addListSelectionListener(new TableSelectionHandler());
		}
		return table;
	}

	/**
	 * This method initializes splitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); // Generated
			splitPane.setDividerSize(8); // Generated
			splitPane.setOneTouchExpandable(true); // Generated
			splitPane.setTopComponent(getScrollPane()); // Generated
			splitPane.setBottomComponent(getEditPanel()); // Generated
			splitPane.setResizeWeight(1.0D); // Generated
		}
		return splitPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private FieldEditorPanel getEditPanel() {
		if (editPanel == null) {
			editPanel = new FieldEditorPanel();
			editPanel.applyButton.setActionCommand(this.ApplyAC);
			editPanel.applyButton.addActionListener(this.getButtonsHandler());
			editPanel.cancelButton.setActionCommand(this.CancelAC);
			editPanel.cancelButton.addActionListener(this.getButtonsHandler());
			editPanel.hideButton.setActionCommand(this.HideAC);
			editPanel.hideButton.addActionListener(this.getButtonsHandler());
		}
		return editPanel;
	}

	/**
	 * This method initializes jToolBar
	 * 
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.setPreferredSize(new Dimension(133, 32)); // Generated
			toolBar.add(getEditButton()); // Generated
			toolBar.add(new JToolBar.Separator(new Dimension(16, 16)));
			toolBar.add(getAddButton()); // Generated
			toolBar.add(getDeleteButton()); // Generated
		}
		return toolBar;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEditButton() {
		if (editButton == null) {
			editButton = new JButton();
			editButton.setText("Edit"); // Generated
			editButton.setActionCommand(this.EditAC);
			editButton.addActionListener(this.getButtonsHandler());
			editButton.setMnemonic(java.awt.event.KeyEvent.VK_E); // Generated
		}
		return editButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText("Add"); // Generated
			addButton.setActionCommand(this.AddAC);
			addButton.addActionListener(this.getButtonsHandler());
			addButton.setMnemonic(java.awt.event.KeyEvent.VK_A); // Generated
		}
		return addButton;
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("Delete"); // Generated
			deleteButton.setActionCommand(this.DeleteAC);
			deleteButton.addActionListener(this.getButtonsHandler());
			deleteButton.setMnemonic(java.awt.event.KeyEvent.VK_D); // Generated
		}
		return deleteButton;
	}

	/**
	 * This method initializes topicTableModel
	 * 
	 * @return table.ObjectBrowserModel
	 */
	public ObjectBrowserModel getTableModel() {
		if (tableModel == null) {
			tableModel = new ObjectBrowserModel();
		}
		return tableModel;
	}

	public void clear() {
		this.tableModel.clear();
	}

	public void enableAll() {
		this.editButton.setEnabled(true);
		this.addButton.setEnabled(true);
		this.deleteButton.setEnabled(true);
		this.table.setEnabled(true);
	}

	public void disableAll() {
		this.editButton.setEnabled(false);
		this.addButton.setEnabled(false);
		this.deleteButton.setEnabled(false);
		this.table.setEnabled(false);
	}

	public void setSelectField(String fieldName) {
		int fieldCount = this.table.getRowCount();
		for (int i = 0; i < fieldCount; i++)
			if (fieldName.equals(this.tableModel.getValueAt(i, 0))) {
				this.table.setRowSelectionInterval(i, i);
				return;
			}
	}

	public void setSupportedTypes(List list) {
		// �趨��DBMS��֧�ֵ�������ͣ�list�����Ѿ�����
		JComboBox types = this.getEditPanel().getTypeComboBox();
		types.removeAllItems();
		ListIterator itor = list.listIterator();
		while (itor.hasNext())
			types.addItem(itor.next());
	}

	private ButtonsActionHandler getButtonsHandler() {
		if (null == buttonsHandler)
			buttonsHandler = new ButtonsActionHandler();
		return buttonsHandler;
	}

	class TableSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			int index = table.getSelectedRow();
			if (-1 == index)
				return;
			editPanel.setFieldInfo((FieldInfo) tableModel.fieldInfos.get(index));
		}

	}

	class ButtonsActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ac = e.getActionCommand();
			if (ac.equals(EditAC)) {
				getSplitPane().setDividerLocation(getSplitPane().getHeight() - 186);
			} else if (ac.equals(AddAC)) {

			} else if (ac.equals(DeleteAC)) {

			} else if (ac.equals(ApplyAC)) {

			} else if (ac.equals(CancelAC)) {

			} else if (ac.equals(HideAC)) {
				getSplitPane().setDividerLocation(getSplitPane().getHeight());
			}
		}
	}
}
